<?php

namespace common\repositories\common;

use common\repositories\BaseRepository;
use common\entities\common\MyscrmFunctionupgradeEntity;
use common\support\StringHelper;

class MyscrmFunctionupgradeRepository extends BaseRepository
{

    private $_myscrmFunctionupgradeEntity;

    public function __construct()
    {
        $this->_myscrmFunctionupgradeEntity = new MyscrmFunctionupgradeEntity();
    }

    /**
     * 添加FunctionUpgrade
     * @param $functionName
     * @param $functionMode
     * @param $organizationId
     * @return bool
     * @throws \Exception
     */
    public function addFunctionUpgrade($functionName, $functionMode, $organizationId){

        $funcUpgrade = MyscrmFunctionupgradeEntity::findOne([
            "organizationId" => $organizationId,
            "functionName" => $functionName,
            "functionMode" => $functionMode
        ]);
        if(empty($funcUpgrade)){
            $funcUpgrade = new MyscrmFunctionupgradeEntity();
            $funcUpgrade['functionUpgradeId'] = StringHelper::uuid();
            $funcUpgrade['organizationId'] = $organizationId;
            $funcUpgrade['functionName'] = $functionName;
            $funcUpgrade['functionMode'] = $functionMode;
            if($funcUpgrade->save()){
                return true;
            }else{
                throw new \Exception(json_encode($funcUpgrade->getFirstErrors(), JSON_UNESCAPED_UNICODE));
            }
        }
        return true;
    }

    /**
     * 删除FunctionUpgrade
     * @param $functionName
     * @param $functionMode
     * @param $organizationId
     * @return bool|false|int
     */
    public function delFunctionUpgrade($functionName, $functionMode, $organizationId){
        $funcUpgrade = MyscrmFunctionupgradeEntity::findOne([
            "organizationId" => $organizationId,
            "functionName" => $functionName,
            "functionMode" => $functionMode
        ]);
        if(!empty($funcUpgrade)){
            return $funcUpgrade->delete();
        }
        return true;
    }

}